<template>
  <div>
    <UserAuthorizationAllocation
      ref="UserAuthorizationAllocationRef"
      @refresh="fetchData"
    />
    <a-card class="general-card" :title="'资源授权'">
      <a-table
        row-key="id"
        :loading="loading"
        :data="renderData"
        :bordered="false"
        :pagination="false"
      >
        <template #columns>
          <a-table-column title="资源名称" data-index="name" />
          <a-table-column
            :title="$t('columns.operations')"
            align="right"
            data-index="operations"
            :width="180"
          >
            <template #cell="{ record }">
              <a-button
                v-if="!record.children"
                type="text"
                size="small"
                @click="handleClickToSave(record)"
              >
                用户授权
              </a-button>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import useLoading from '@/hooks/loading';
  import { TableData } from '@arco-design/web-vue';
  import { getNoAuthorityResource } from '@/api/resources';
  import UserAuthorizationAllocation from './user-authorization-allocation.vue';

  const { loading, setLoading } = useLoading();
  const renderData = ref<TableData[]>([]);

  const UserAuthorizationAllocationRef = ref();
  const fetchData = async () => {
    setLoading(true);
    try {
      const { data } = await getNoAuthorityResource();
      renderData.value = [
        {
          id: '1',
          code: 'PICTURE',
          name: '图片',
          children: data.PICTURE,
        },
        {
          id: '2',
          code: 'ICON',
          name: 'icon图标',
          children: data.ICON,
        },
        {
          id: '3',
          code: 'BACKENDURL',
          name: '后端路径',
          children: data.BACKENDURL,
        },
      ];
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };

  // };
  fetchData();
  const handleClickToSave = (obj: any) => {
    UserAuthorizationAllocationRef.value.handleChangeVisible(obj);
  };

  // fetchPortalData();
</script>

<script lang="ts">
  export default {
    name: 'MenuList',
  };
</script>

<style scoped lang="less">
  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
</style>
